var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

router.get('/api/getData', function(req, res, next) {
  var data = {
    title: 'Express',
    description: 'This is a sample Express application.',
    items: [
      { id: 1, name: 'Item 1','url':'http://10.11.5.167:3300/combined.mp4' },
      { id: 2, name: 'Item 2' ,'url':'http://10.11.5.167:3300/行为统计饼状图.png' },
      { id: 3, name: 'Item 3', 'url':'http://10.11.5.167:3300/行为统计可视化.png' },
      { id: 4, name: 'Item 4', 'url':'http://10.11.5.167:3300/行为数据统计.csv' },
      { id: 5, name: 'Item 5', 'url':'http://10.11.5.167:3300/行为统计雷达图.png' }
    
    ]
  };
  res.json(data);
});

router.post('/saveData', function(req, res, next) {
  console.log('Received data:', req.body);
  
  console.log('Received data:', req.body);
  var resposnseData = {
    code:1,
    status: 'success',
    message: 'Data saved successfully',
  };
  res.json(resposnseData);
});

// router.post('/register', async (req, res) => {
//   const { username, password, userType } = req.body;
//   console.log('收到注册请求:', { username, password, userType });

//   // 检查必要参数是否为空
//   if (!username || !password || !userType) {
//     return res.status(400).json({ success: false, message: '缺少必要的注册参数' });
//   }

//   try {
//     console.log('开始查询数据库...');
//     // 查询是否有重复用户名
//     const [existingUsers=[]] = await query('SELECT * FROM users WHERE username = ?', [username]);
//     console.log('数据库查询完成:', existingUsers);
    
//     // 检查数据库查询结果是否有效
//     if (existingUsers.length>0) {
//       return res.status(400).json({ success: false, message: '用户名已存在' });
//     }

//     // 生成一个唯一的账号
//     let account = generateAccount();
//     let [existingAccounts] = await query('SELECT * FROM users WHERE account = ?', [account]);
    
//     // 检查账号是否已存在，确保唯一
//     while (existingAccounts && existingAccounts.length > 0) {
//       account = generateAccount();
//       [existingAccounts] = await query('SELECT * FROM users WHERE account = ?', [account]);
//     }

//     // 将用户信息插入数据库
//     await query('INSERT INTO users (username, password, account, userType) VALUES (?, ?, ?, ?)', [username, password, account, userType]);
//     console.log('注册成功:', { username, account, userType });

//     // 返回成功响应
//     res.status(201).json({ success: true, message: '注册成功', user: { username, account, userType } });
//   } catch (error) {
//     console.error('Register error:', error);
//     res.status(500).json({ success: false, message: '注册失败', error: error.message });
//   }
// });


 module.exports = router;
